# lisp grammar language lexer+parser to use with packcc parser generator
%prefix "lisp"

%source{

#include <stdio.h>

static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };

#define PCC_DEBUG(event, rule, level, pos, buffer, length) \
    fprintf(stdout, "%*s%s %s @%d [%.*s]\n", (int)(level * 2), "", dbg_str[event], rule, (int)pos, (int)length,  buffer); fflush(stdout)

    /* NOTE: To guarantee the output order, stderr, which can lead a race condition with stdout, is not used. */

}

file <-	_ expression* _ !.

expression	<- _ (atom	/ list) _

atom	<- (number	/ symbol) _

number	<- ('-' / '+')?[0-9]+

symbol	<- [a-zA-Z]+[a-ZA-Z0-9]*

list	<- ( '(' expression* ')' ) _

sp	<-	[ \t]

_	<- (sp / endofline)*

endofline	<-	( '\r\n' / '\n' / '\r' / '\n\r' )

%%

int main() {
    lisp_context_t *ctx=lisp_create(NULL);
    while (lisp_parse(ctx, NULL)){;}
    lisp_destroy(ctx);
    return 0;
}

